<template>
  <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="患者信息" prop="fileNumber">
            <el-input
              v-model="queryParams.fileNumber"
              placeholder="档案号/姓名/手机号码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
<!--          <el-form-item label="名称" prop="name">-->
<!--            <el-input-->
<!--              v-model="queryParams.name"-->
<!--              placeholder="请输入检查项名称"-->
<!--              clearable-->
<!--              size="small"-->
<!--              @keyup.enter.native="handleQuery"-->
<!--            />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="性别" prop="sex">-->
<!--            <el-select v-model="queryParams.sex" placeholder="性别" clearable size="small">-->
<!--              <el-option-->
<!--                v-for="item in sexOptions"-->
<!--                :key="item.value"-->
<!--                :label="item.label"-->
<!--                :value="item.value">-->
<!--              </el-option>-->
<!--            </el-select>-->
<!--          </el-form-item>-->
<!--          <el-form-item label="类型" prop="type">-->
<!--            <el-select v-model="queryParams.type" placeholder="类型" clearable size="small">-->
<!--              <el-option-->
<!--                v-for="item in typeOptions"-->
<!--                :key="item.value"-->
<!--                :label="item.label"-->
<!--                :value="item.value">-->
<!--              </el-option>-->
<!--            </el-select>-->
<!--          </el-form-item>-->
<!--          <el-form-item>-->
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
<!--          </el-form-item>-->
        </el-form>


        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增</el-button>
          </el-col>
<!--          <el-col :span="1.5">-->
<!--            <el-button-->
<!--              type="danger"-->
<!--              plain-->
<!--              icon="el-icon-delete"-->
<!--              size="mini"-->
<!--              :disabled="multiple"-->
<!--              @click="handleDelete"-->
<!--            >删除</el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button-->
<!--              type="success"-->
<!--              plain-->
<!--              icon="el-icon-edit"-->
<!--              size="mini"-->
<!--              :disabled="single"-->
<!--              @click="handleUpdate"-->
<!--            >修改</el-button>-->
<!--          </el-col>-->
<!--        </el-row>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button-->
<!--              type="warning"-->
<!--              plain-->
<!--              icon="el-icon-download"-->
<!--              size="mini"-->
<!--              @click="handleExport"-->
<!--              v-hasPermi="['system:post:export']"-->
<!--            >导出</el-button>-->
<!--          </el-col>-->
<!--          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
        </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="预约日期" align="center" prop="orderDate" />
      <el-table-column label="档案号" align="center" prop="fileNumber" />
      <el-table-column label="姓名" align="center" prop="name"/>

      <el-table-column label="手机号码" align="center" prop="phoneNumber" />
      <el-table-column label="预约类型" align="center" prop="orderType">
                <template slot-scope="scope">
                  <p v-if="scope.row.orderType == '0'">微信预约</p>
                  <p v-if="scope.row.orderType == '2'">电话预约</p>
                </template>
      </el-table-column>
      <el-table-column label="预约状态" align="center" prop="type" width="180" >
        <template slot-scope="scope">
          <p v-if="scope.row.orderStatus == '0'">未预约</p>
          <p v-if="scope.row.orderStatus == '1'">取消预约</p>
          <p v-if="scope.row.orderStatus == '2'">已预约</p>
        </template>
      </el-table-column>
            <el-table-column label="预约状态操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  v-if="scope.row.orderStatus=='0'"
                  @click="handleUpdateComfirm(scope.row)"
                >确认预约</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  v-if="scope.row.orderStatus=='0'"
                  @click="handleUpdateCancel(scope.row)"
                >取消预约</el-button>
              </template>
            </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

<!--     添加或修改岗位对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" label-width="0px" append-to-body>
          <template>

          </template>


          <el-form ref="form" :model="form"  label-width="80px">
            <el-form-item label="患者信息" prop="fileNumber">
              <el-select
                @change="ChangeBind"
                style="width: 300px"
                v-model="value"
                filterable
                remote
                reserve-keyword
                placeholder="姓名/手机号码/档案号"
                :remote-method="remoteMethod"
                :loading="loading1">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="档案号" prop="fileNumber">
              <el-input v-model="form.fileNumber" :readonly="true"/>
            </el-form-item>
            <el-form-item label="预约日期" prop="orderDate">
              <el-date-picker
                v-model="form.orderDate"
                type="date"
                placeholder="选择日期"/>
            </el-form-item>
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" :readonly="true"/>
            </el-form-item>
            <el-form-item label="手机号码" prop="phoneNumber">
              <el-input v-model="form.phoneNumber" :readonly="true"/>
            </el-form-item>
            <el-form-item label="预约类型" prop="orderType">
              <el-radio-group v-model="form.orderType" :disabled="true">
                <el-radio label="0">微信预约</el-radio>
                <el-radio label="2">电话预约</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="套餐" prop="setmealId">
              <el-input v-model="form.setmealId"/>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
  </div>
</template>

<script>
import { listOrder, updateComfirmOrderStatus, updateCancelOrderStatus, listOrder1, addOrder} from "@/api/reserve/order";

export default {
  name: "Order",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      disabledDate(time) {
        return time.getTime() > Date.now();
      },

      // 新增远程搜索
      options: [],
      value: [],
      loading1: false,


      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预约表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fileNumber: undefined,
        orderStatus: undefined,
        name: undefined,
        memberId: undefined,
        orderType: undefined,
        orderDate: undefined
      },
      // // 选择性别查询参数
      // sexOptions: [{
      //   value: '0',
      //   label: '全部'
      // }, {
      //   value: '1',
      //   label: '男'
      // }, {
      //   value: '2',
      //   label: '女'
      // }],
      // // 类型选择查询参数
      // typeOptions: [{
      //   value: '1',
      //   label: '检查'
      // }, {
      //   value: '2',
      //   label: '检验'
      // }],
      // // 新增检测


    };
  },
  created() {
    this.getList();
  },
  // 新增远程搜索下拉框数据显示
  mounted() {
    listOrder1(this.queryParams).then(response => {
      console.log(response);
      this.list = response.map(item => {
        return { value: `${item.name}-${item.phoneNumber}-${item.fileNumber}-${item.orderType}-${item.id}`, label: `手机号:${item.phoneNumber}--姓名:${item.name}`};
      });
    });
    // this.list = this.states.map(item => {
    //   return { value: `value:${item}`, label: `label:${item}` };
    // });
  },
  methods: {
    ChangeBind(data) {
      let userinfo = data.split("-");
      let name = userinfo[0];
      let phone = userinfo[1];
      let fileNumber = userinfo[2];
      let orderType = userinfo[3];
      let memberId = userinfo[4];
      this.form.name = name;
      this.form.phoneNumber = phone;
      this.form.fileNumber = fileNumber;
      this.form.orderType = '2';
      this.form.memberId = memberId;
    },
    // 新增远程搜索
    remoteMethod(query) {
      if (query !== "") {
        this.loading1 = true;
        setTimeout(() => {
          this.loading1 = false;
          listOrder1(this.queryParams).then(response => {
            console.log(response);
            this.options =  this.list.filter(item => {
              return item.label.toLowerCase()
                .indexOf(query.toLowerCase()) > -1;
            });
            this.loading = false;
          });
        }, 200);

      } else {
        this.options = [];
      }
    },

    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.postList = response.rows;
        console.log(response)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderType: "2"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      console.log(this.queryParams)
      this.getList();
    },
    /** 重置按钮操作 */
     resetQuery() {
       this.resetForm("queryForm");
       this.handleQuery();
     },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log(selection);
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预约";
    },
    /** 确认预约按钮操作 */
    handleUpdateComfirm(row) {
      let order = row
      this.$modal.confirm('是否确认预约？').then(function() {
        return updateComfirmOrderStatus(order);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("预约成功");
      }).catch(() => {});
    },
    /** 取消预约按钮操作 */
    handleUpdateCancel(row) {
      let order = row
      this.$modal.confirm('是否取消预约？').then(function() {
        return updateCancelOrderStatus(order);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("取消预约成功");
      }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm: function() {
      console.log(this.form);
      this.form.orderDateForm = this.form.orderDate

          addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });




    },

  }
};
</script>
